package com.star.common.sms.factory;

import com.star.common.sms.contract.enums.SmsErrorEnum;
import com.star.common.sms.contract.enums.SmsTypeEnum;
import com.star.common.sms.contract.exception.SmsException;
import com.star.common.sms.service.SmsClient;
import lombok.extern.slf4j.Slf4j;

import java.util.Map;
import java.util.Objects;

/**
 * 短信工厂
 *
 * @author zhouhengzhe
 * @version 1.0
 */
@Slf4j
public class SmsFactory {
    private final Map<String, SmsClient> smsClientMap;

    public SmsFactory(Map<String, SmsClient> smsClientMap) {
        this.smsClientMap = smsClientMap;
    }

    /**
     * 生产短信发送方
     *
     * @param provider 短信类型，
     * @return 短信发送方
     * @see SmsTypeEnum
     */
    public SmsClient buildSmsClient(String provider) {
        SmsClient smsClient = smsClientMap.get(provider);
        if (Objects.isNull(smsClient)) {
            log.error("当前的【{}】的对应的yml配置为空，或者【{}】类型不存在", provider, provider);
            throw new SmsException(SmsErrorEnum.CREATE_SMS_FACTORY_ERROR);
        }
        return smsClient;
    }

    /**
     * 获取阿里云短信发送
     */
    public SmsClient getAliyunSmsClient() {
        return buildSmsClient(SmsTypeEnum.ALIYUN.getCode());
    }

    /**
     * 获取博士通短信发送
     */
    public SmsClient getBoshitongSmsClient() {
        return buildSmsClient(SmsTypeEnum.BOSHITONG.getCode());
    }
}
